package com.datasecops.scannerservices.util;

import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

/**
 * 字节转换工具类
 * @author: SunLingzhi
 * @time: 2022/8/9 14:47
 */
public class ByteStreamHelper {

    /**
     * 返回值字节流解析
     * @Description
     * @author SunLingzhi
     * @time 2022/8/9 13:43
     * @param
     * @return
     */
    public static byte[] inputStreamTOByte(InputStream in) throws IOException {
        int BUFFER_SIZE = 4096;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        int count = -1;
        while((count = in.read(data,0,BUFFER_SIZE)) != -1)
            outStream.write(data, 0, count);

        data = null;
        return outStream.toByteArray();
    }

    /**
     * 格式化字节流
     * @Description
     * @author SunLingzhi
     * @time 2022/8/9 13:48
     * @param
     * @return
     */
    public static byte[] formatParamDataFlow(String strParam) throws IOException {
        if(null==strParam){
            return null;
        }
        byte[] paramData = strParam.getBytes("UTF-16");

        int uBufLen = 1 + 1 + 4 + paramData.length + 1 +4 + paramData.length + 1 +1;

        int iPosIdx =0;
        byte[] paramBuf = new byte[uBufLen];
        paramBuf[iPosIdx] = 'W';
        iPosIdx +=1;

        paramBuf[iPosIdx] = 1;//Big-Endian,0:little-endian
        iPosIdx +=1;

        byte[] dataLenByteArr = intToByteArr(paramData.length);

        System.arraycopy(dataLenByteArr,0,paramBuf,iPosIdx,dataLenByteArr.length);
        iPosIdx +=4;

        System.arraycopy(paramData,0,paramBuf,iPosIdx,paramData.length);
        iPosIdx += paramData.length;

        paramBuf[iPosIdx] = 'M';
        iPosIdx += 1;

        dataLenByteArr = intToByteArr(paramData.length);
        System.arraycopy(dataLenByteArr,0,paramBuf,iPosIdx,dataLenByteArr.length);
        iPosIdx +=4;

        System.arraycopy(paramData,0,paramBuf,iPosIdx,paramData.length);
        iPosIdx += paramData.length;

        paramBuf[iPosIdx] = 'e';
        iPosIdx +=1;
        paramBuf[iPosIdx] = 'E';
        iPosIdx +=1;
        return paramBuf;
    }

    public static byte[] intToByteArr(int x){
        byte[] arr = new byte[4];
        arr[3]= (byte)(x & 0xff);
        arr[2]= (byte)(x>>8 & 0xff);
        arr[1]= (byte)(x>>16 & 0xff);
        arr[0]= (byte)(x>>24 & 0xff);
        return arr;
    }


     /**
      * 截取获取返回数据
      * @Description
      * @author SunLingzhi
      * @time 2022/8/10 17:51
      * @param
      * @return
      */
    public static String getFailureDetails(byte[] retStream) {
        byte[] data = new byte[retStream.length-9];
        System.arraycopy(retStream, 9, data, 0, retStream.length-11);
        return new String(data, StandardCharsets.UTF_16).trim();
    }

     /**
      * xml字符串转换
      * @Description
      * @author SunLingzhi
      * @time 2022/8/11 16:57
      * @param
      * @param node
      * @return
      */
    public static String toXmlString(Element node) throws IOException {
        OutputFormat oFormat = OutputFormat.createPrettyPrint();
        oFormat.setEncoding("UTF-16");
        StringWriter sWriter = new StringWriter();
        XMLWriter xWriter = new XMLWriter(sWriter, oFormat);
        xWriter.write(node); //为document
        xWriter.flush();
        xWriter.close();
        return sWriter.toString();
    }

}
